package entities;

import view.ChessBoard;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * Author: Mr.Zhao, Time: 2018/4/28/9:24
 * Description: 車
 */
public class Ju extends BasePiece {

    public Ju(int x, int y, Color color) {
        super(x, y, color);
        this.pieceName = "車";
    }

    /**
     * 根据"車"的规则以及棋盘已有的棋子来确定"車"的行走规则
     * 分别从棋子的当前位置向上下左右寻找，如果前面有我方棋子挡路，则跳过随后的所有坐标点
     *
     * @param pointPieceMap 当前棋盘中的所有坐标-棋子键值对
     * @author 赵永远
     */

    @Override
    public void updateAvailablePoints(Map<Point, BasePiece> pointPieceMap) {
        availablePoints.clear();
        int x = getPosition().x;
        int y = getPosition().y;

        /* 从当前位置向上 */
        for (int yi = y - 1; yi >= 0; yi--) {
            //取出当前坐标的棋子
            BasePiece piece = pointPieceMap.get(new Point(x, yi));
            //该处没有棋子，将坐标加入可移动点
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(x, yi));
            }
            //该处有棋子且颜色为我方棋子颜色，不再添加，直接跳出循环
            else if (piece.getColor() == this.getColor()) {
                break;
            }
            //该处棋子为敌方棋子颜色，先将此棋子坐标加入，再跳出循环
            else {
                this.availablePoints.add(new AvailablePoint(x, yi));
                break;
            }
        }

        /* 从当前位置向下 */
        for (int yi = y + 1; yi <= ChessBoard.HEIGHT; yi++) {
            BasePiece piece = pointPieceMap.get(new Point(x, yi));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(x, yi));
            } else if (piece.getColor() == this.getColor()) {
                break;
            } else {
                this.availablePoints.add(new AvailablePoint(x, yi));
                break;
            }
        }
        /* 从当前位置向左 */
        for (int xi = x - 1; xi >= 0; xi--) {
            BasePiece piece = pointPieceMap.get(new Point(xi, y));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(xi, y));
            } else if (piece.getColor() == this.getColor()) {
                break;
            } else {
                this.availablePoints.add(new AvailablePoint(xi, y));
                break;
            }
        }

        /* 从当前位置向右 */
        for (int xi = x + 1; xi <= ChessBoard.WIDTH; xi++) {

            BasePiece piece = pointPieceMap.get(new Point(xi, y));
            if (piece == null) {
                this.availablePoints.add(new AvailablePoint(xi, y));
            } else if (piece.getColor() == this.getColor()) {
                break;
            } else {
                this.availablePoints.add(new AvailablePoint(xi, y));
                break;
            }
        }

    }

}
